package yunjiao.javatutorials.guava.concurrent;

import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.SettableFuture;

/**
 * 异步任务协调
 *
 * @author yangyunjiao
 */
public class AsyncCoordinator {
    private final SettableFuture<Boolean> initializationFuture = SettableFuture.create();

    public void initialize() {
        new Thread(() -> {
            try {
                // 模拟初始化过程
                Thread.sleep(3000);
                System.out.println("系统初始化完成");
                initializationFuture.set(true);
            } catch (Exception e) {
                initializationFuture.setException(e);
            }
        }).start();
    }

    public ListenableFuture<Boolean> whenInitialized() {
        return initializationFuture;
    }
}
